{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d079be69",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "043f6e86",
   "metadata": {},
   "outputs": [],
   "source": [
    "dir = 'E:\\\\HQData\\\\future\\\\'\n",
    "\n",
    "exchanges=[\"DCE\", \"CZCE\", \"SHFE\"]\n",
    "\n",
    "\n",
    "files = list()\n",
    "files.append(\"fut_czce\")\n",
    "files.append(\"fut_dce\")\n",
    "files.append(\"fut_ine\")\n",
    "files.append(\"fut_shf\")\n",
    "\n",
    "sort_values=[\"exchange\",\"ts_code\"]\n",
    "\n",
    "#files.append(\"fut_czce\")\n",
    "#files.append(\"fut_czce\")\n",
    "#files.append(\"fut_czce\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5fee1bd8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_7416\\420476539.py:6: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  df_all = df_all.append(pd.read_csv(f\"{dir}{file}.csv\"))\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_7416\\420476539.py:6: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  df_all = df_all.append(pd.read_csv(f\"{dir}{file}.csv\"))\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_7416\\420476539.py:6: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  df_all = df_all.append(pd.read_csv(f\"{dir}{file}.csv\"))\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_7416\\420476539.py:6: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  df_all = df_all.append(pd.read_csv(f\"{dir}{file}.csv\"))\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>exchange</th>\n",
       "      <th>name</th>\n",
       "      <th>fut_code</th>\n",
       "      <th>multiplier</th>\n",
       "      <th>trade_unit</th>\n",
       "      <th>per_unit</th>\n",
       "      <th>quote_unit</th>\n",
       "      <th>quote_unit_desc</th>\n",
       "      <th>d_mode_desc</th>\n",
       "      <th>list_date</th>\n",
       "      <th>delist_date</th>\n",
       "      <th>d_month</th>\n",
       "      <th>last_ddate</th>\n",
       "      <th>trade_time_desc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>AP2107.ZCE</td>\n",
       "      <td>AP107</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>苹果2107</td>\n",
       "      <td>AP</td>\n",
       "      <td>NaN</td>\n",
       "      <td>吨</td>\n",
       "      <td>10.0</td>\n",
       "      <td>人民币元/吨</td>\n",
       "      <td>1人民币元/吨</td>\n",
       "      <td>实物交割</td>\n",
       "      <td>20200715</td>\n",
       "      <td>20210714</td>\n",
       "      <td>202107</td>\n",
       "      <td>20210719.0</td>\n",
       "      <td>周一至周五(北京时间 法定节假日除外)上午9:00-11:30 下午13:30-15:00 ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>TA1001.ZCE</td>\n",
       "      <td>TA001</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>PTA1001</td>\n",
       "      <td>TA</td>\n",
       "      <td>NaN</td>\n",
       "      <td>吨</td>\n",
       "      <td>5.0</td>\n",
       "      <td>人民币元/吨</td>\n",
       "      <td>2人民币元/吨</td>\n",
       "      <td>实物交割</td>\n",
       "      <td>20090119</td>\n",
       "      <td>20100115</td>\n",
       "      <td>201001</td>\n",
       "      <td>20100119.0</td>\n",
       "      <td>上午9:00-11:30,下午13:30-15:00,下午21:00-23:00(夜盘)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>ZC2506.ZCE</td>\n",
       "      <td>ZC506</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>动力煤2506</td>\n",
       "      <td>ZC</td>\n",
       "      <td>NaN</td>\n",
       "      <td>吨</td>\n",
       "      <td>100.0</td>\n",
       "      <td>人民币元/吨</td>\n",
       "      <td>0.2人民币元/吨</td>\n",
       "      <td>实物交割</td>\n",
       "      <td>20240611</td>\n",
       "      <td>20250609</td>\n",
       "      <td>202506</td>\n",
       "      <td>20250612.0</td>\n",
       "      <td>上午9:00-11:30，下午1:30-3:00及交易所规定的其他交易时间,下午21:00-...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>TC1604.ZCE</td>\n",
       "      <td>TC604</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>动力煤1604</td>\n",
       "      <td>TC</td>\n",
       "      <td>NaN</td>\n",
       "      <td>吨</td>\n",
       "      <td>200.0</td>\n",
       "      <td>人民币元/吨</td>\n",
       "      <td>0.2人民币元/吨</td>\n",
       "      <td>实物交割</td>\n",
       "      <td>20150409</td>\n",
       "      <td>20160408</td>\n",
       "      <td>201604</td>\n",
       "      <td>20160412.0</td>\n",
       "      <td>上午9:00-11:30,下午13:30-15:00,以及交易所规定的其他交易时间,下午21...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>WT0601.ZCE</td>\n",
       "      <td>WT601</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>硬麦0601</td>\n",
       "      <td>WT</td>\n",
       "      <td>NaN</td>\n",
       "      <td>吨</td>\n",
       "      <td>10.0</td>\n",
       "      <td>人民币元/吨</td>\n",
       "      <td>1人民币元/吨</td>\n",
       "      <td>实物交割</td>\n",
       "      <td>20050201</td>\n",
       "      <td>20060119</td>\n",
       "      <td>200601</td>\n",
       "      <td>20060127.0</td>\n",
       "      <td>上午9:00-11:30，下午13:30-15:00，以及交易所规定的其他时间</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0     ts_code symbol exchange     name fut_code  multiplier  \\\n",
       "0           0  AP2107.ZCE  AP107     CZCE   苹果2107       AP         NaN   \n",
       "1           1  TA1001.ZCE  TA001     CZCE  PTA1001       TA         NaN   \n",
       "2           2  ZC2506.ZCE  ZC506     CZCE  动力煤2506       ZC         NaN   \n",
       "3           3  TC1604.ZCE  TC604     CZCE  动力煤1604       TC         NaN   \n",
       "4           4  WT0601.ZCE  WT601     CZCE   硬麦0601       WT         NaN   \n",
       "\n",
       "  trade_unit  per_unit quote_unit quote_unit_desc d_mode_desc  list_date  \\\n",
       "0          吨      10.0     人民币元/吨         1人民币元/吨        实物交割   20200715   \n",
       "1          吨       5.0     人民币元/吨         2人民币元/吨        实物交割   20090119   \n",
       "2          吨     100.0     人民币元/吨       0.2人民币元/吨        实物交割   20240611   \n",
       "3          吨     200.0     人民币元/吨       0.2人民币元/吨        实物交割   20150409   \n",
       "4          吨      10.0     人民币元/吨         1人民币元/吨        实物交割   20050201   \n",
       "\n",
       "   delist_date  d_month  last_ddate  \\\n",
       "0     20210714   202107  20210719.0   \n",
       "1     20100115   201001  20100119.0   \n",
       "2     20250609   202506  20250612.0   \n",
       "3     20160408   201604  20160412.0   \n",
       "4     20060119   200601  20060127.0   \n",
       "\n",
       "                                     trade_time_desc  \n",
       "0  周一至周五(北京时间 法定节假日除外)上午9:00-11:30 下午13:30-15:00 ...  \n",
       "1       上午9:00-11:30,下午13:30-15:00,下午21:00-23:00(夜盘)  \n",
       "2  上午9:00-11:30，下午1:30-3:00及交易所规定的其他交易时间,下午21:00-...  \n",
       "3  上午9:00-11:30,下午13:30-15:00,以及交易所规定的其他交易时间,下午21...  \n",
       "4            上午9:00-11:30，下午13:30-15:00，以及交易所规定的其他时间  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_all = pd.DataFrame()\n",
    "\n",
    "#df_dce = pd.read_csv(f\"{dir}fut_czce.csv\")\n",
    "\n",
    "for file in files:\n",
    "    df_all = df_all.append(pd.read_csv(f\"{dir}{file}.csv\"))\n",
    "df_all.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "22e6cd14",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9074\n"
     ]
    }
   ],
   "source": [
    "print(len(df_all))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4e6a914d",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_trade_all = df_all[df_all[\"last_ddate\"] > 20250501]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "32b3de7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "730\n"
     ]
    }
   ],
   "source": [
    "print(len(df_trade_all))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1b39e43e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>exchange</th>\n",
       "      <th>name</th>\n",
       "      <th>fut_code</th>\n",
       "      <th>multiplier</th>\n",
       "      <th>trade_unit</th>\n",
       "      <th>per_unit</th>\n",
       "      <th>quote_unit</th>\n",
       "      <th>quote_unit_desc</th>\n",
       "      <th>d_mode_desc</th>\n",
       "      <th>list_date</th>\n",
       "      <th>delist_date</th>\n",
       "      <th>d_month</th>\n",
       "      <th>last_ddate</th>\n",
       "      <th>trade_time_desc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>77</td>\n",
       "      <td>AP2505.ZCE</td>\n",
       "      <td>AP505</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>苹果2505</td>\n",
       "      <td>AP</td>\n",
       "      <td>NaN</td>\n",
       "      <td>吨</td>\n",
       "      <td>10.0</td>\n",
       "      <td>人民币元/吨</td>\n",
       "      <td>1人民币元/吨</td>\n",
       "      <td>实物交割</td>\n",
       "      <td>20240520</td>\n",
       "      <td>20250519</td>\n",
       "      <td>202505</td>\n",
       "      <td>20250522.0</td>\n",
       "      <td>周一至周五(北京时间法定节假日除外)上午9:00-11:30下午13:30-15:00及交易...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2276</th>\n",
       "      <td>2276</td>\n",
       "      <td>AP2510.ZCE</td>\n",
       "      <td>AP510</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>苹果2510</td>\n",
       "      <td>AP</td>\n",
       "      <td>NaN</td>\n",
       "      <td>吨</td>\n",
       "      <td>10.0</td>\n",
       "      <td>人民币元/吨</td>\n",
       "      <td>1人民币元/吨</td>\n",
       "      <td>实物交割</td>\n",
       "      <td>20241022</td>\n",
       "      <td>20251022</td>\n",
       "      <td>202510</td>\n",
       "      <td>20251027.0</td>\n",
       "      <td>周一至周五(北京时间法定节假日除外)上午9:00-11:30下午13:30-15:00及交易...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2070</th>\n",
       "      <td>2070</td>\n",
       "      <td>AP2511.ZCE</td>\n",
       "      <td>AP511</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>苹果2511</td>\n",
       "      <td>AP</td>\n",
       "      <td>NaN</td>\n",
       "      <td>吨</td>\n",
       "      <td>10.0</td>\n",
       "      <td>人民币元/吨</td>\n",
       "      <td>1人民币元/吨</td>\n",
       "      <td>实物交割</td>\n",
       "      <td>20241115</td>\n",
       "      <td>20251114</td>\n",
       "      <td>202511</td>\n",
       "      <td>20251119.0</td>\n",
       "      <td>周一至周五(北京时间法定节假日除外)上午9:00-11:30下午13:30-15:00及交易...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2471</th>\n",
       "      <td>2471</td>\n",
       "      <td>AP2512.ZCE</td>\n",
       "      <td>AP512</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>苹果2512</td>\n",
       "      <td>AP</td>\n",
       "      <td>NaN</td>\n",
       "      <td>吨</td>\n",
       "      <td>10.0</td>\n",
       "      <td>人民币元/吨</td>\n",
       "      <td>1人民币元/吨</td>\n",
       "      <td>实物交割</td>\n",
       "      <td>20241216</td>\n",
       "      <td>20251212</td>\n",
       "      <td>202512</td>\n",
       "      <td>20251217.0</td>\n",
       "      <td>周一至周五(北京时间法定节假日除外)上午9:00-11:30下午13:30-15:00及交易...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2232</th>\n",
       "      <td>2232</td>\n",
       "      <td>AP2601.ZCE</td>\n",
       "      <td>AP601</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>苹果2601</td>\n",
       "      <td>AP</td>\n",
       "      <td>NaN</td>\n",
       "      <td>吨</td>\n",
       "      <td>10.0</td>\n",
       "      <td>人民币元/吨</td>\n",
       "      <td>1人民币元/吨</td>\n",
       "      <td>实物交割</td>\n",
       "      <td>20250116</td>\n",
       "      <td>20260114</td>\n",
       "      <td>202601</td>\n",
       "      <td>20260119.0</td>\n",
       "      <td>周一至周五(北京时间法定节假日除外)上午9:00-11:30下午13:30-15:00及交易...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Unnamed: 0     ts_code symbol exchange    name fut_code  multiplier  \\\n",
       "77            77  AP2505.ZCE  AP505     CZCE  苹果2505       AP         NaN   \n",
       "2276        2276  AP2510.ZCE  AP510     CZCE  苹果2510       AP         NaN   \n",
       "2070        2070  AP2511.ZCE  AP511     CZCE  苹果2511       AP         NaN   \n",
       "2471        2471  AP2512.ZCE  AP512     CZCE  苹果2512       AP         NaN   \n",
       "2232        2232  AP2601.ZCE  AP601     CZCE  苹果2601       AP         NaN   \n",
       "\n",
       "     trade_unit  per_unit quote_unit quote_unit_desc d_mode_desc  list_date  \\\n",
       "77            吨      10.0     人民币元/吨         1人民币元/吨        实物交割   20240520   \n",
       "2276          吨      10.0     人民币元/吨         1人民币元/吨        实物交割   20241022   \n",
       "2070          吨      10.0     人民币元/吨         1人民币元/吨        实物交割   20241115   \n",
       "2471          吨      10.0     人民币元/吨         1人民币元/吨        实物交割   20241216   \n",
       "2232          吨      10.0     人民币元/吨         1人民币元/吨        实物交割   20250116   \n",
       "\n",
       "      delist_date  d_month  last_ddate  \\\n",
       "77       20250519   202505  20250522.0   \n",
       "2276     20251022   202510  20251027.0   \n",
       "2070     20251114   202511  20251119.0   \n",
       "2471     20251212   202512  20251217.0   \n",
       "2232     20260114   202601  20260119.0   \n",
       "\n",
       "                                        trade_time_desc  \n",
       "77    周一至周五(北京时间法定节假日除外)上午9:00-11:30下午13:30-15:00及交易...  \n",
       "2276  周一至周五(北京时间法定节假日除外)上午9:00-11:30下午13:30-15:00及交易...  \n",
       "2070  周一至周五(北京时间法定节假日除外)上午9:00-11:30下午13:30-15:00及交易...  \n",
       "2471  周一至周五(北京时间法定节假日除外)上午9:00-11:30下午13:30-15:00及交易...  \n",
       "2232  周一至周五(北京时间法定节假日除外)上午9:00-11:30下午13:30-15:00及交易...  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_trade_all = df_trade_all.sort_values([\"exchange\",\"ts_code\"])\n",
    "df_trade_all.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1a776d96",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_trade_all.to_csv(f\"{dir}trade_all_contracts.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "03f9fbea",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "mainfiles = list()\n",
    "mainfiles.append(\"main_czce\")\n",
    "mainfiles.append(\"main_dce\")\n",
    "mainfiles.append(\"main_ine\")\n",
    "mainfiles.append(\"main_shf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e97fe32f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_7416\\3804287891.py:4: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  df_main = df_main.append(pd.read_csv(f\"{dir}{file}.csv\"))\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_7416\\3804287891.py:4: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  df_main = df_main.append(pd.read_csv(f\"{dir}{file}.csv\"))\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_7416\\3804287891.py:4: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  df_main = df_main.append(pd.read_csv(f\"{dir}{file}.csv\"))\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>ts_code</th>\n",
       "      <th>symbol</th>\n",
       "      <th>exchange</th>\n",
       "      <th>name</th>\n",
       "      <th>fut_code</th>\n",
       "      <th>multiplier</th>\n",
       "      <th>trade_unit</th>\n",
       "      <th>per_unit</th>\n",
       "      <th>quote_unit</th>\n",
       "      <th>quote_unit_desc</th>\n",
       "      <th>d_mode_desc</th>\n",
       "      <th>list_date</th>\n",
       "      <th>delist_date</th>\n",
       "      <th>d_month</th>\n",
       "      <th>last_ddate</th>\n",
       "      <th>trade_time_desc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>SH.ZCE</td>\n",
       "      <td>SH</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>烧碱主力</td>\n",
       "      <td>SH</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>SHL.ZCE</td>\n",
       "      <td>SHL</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>烧碱连续</td>\n",
       "      <td>SH</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>CJL.ZCE</td>\n",
       "      <td>CJL</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>红枣连续</td>\n",
       "      <td>CJ</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>CJ.ZCE</td>\n",
       "      <td>CJ</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>红枣主力</td>\n",
       "      <td>CJ</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>UR.ZCE</td>\n",
       "      <td>UR</td>\n",
       "      <td>CZCE</td>\n",
       "      <td>尿素主力</td>\n",
       "      <td>UR</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0  ts_code symbol exchange  name fut_code  multiplier  trade_unit  \\\n",
       "0           0   SH.ZCE     SH     CZCE  烧碱主力       SH         NaN         NaN   \n",
       "1           1  SHL.ZCE    SHL     CZCE  烧碱连续       SH         NaN         NaN   \n",
       "2           2  CJL.ZCE    CJL     CZCE  红枣连续       CJ         NaN         NaN   \n",
       "3           3   CJ.ZCE     CJ     CZCE  红枣主力       CJ         NaN         NaN   \n",
       "4           4   UR.ZCE     UR     CZCE  尿素主力       UR         NaN         NaN   \n",
       "\n",
       "   per_unit  quote_unit  quote_unit_desc  d_mode_desc  list_date  delist_date  \\\n",
       "0       NaN         NaN              NaN          NaN        NaN          NaN   \n",
       "1       NaN         NaN              NaN          NaN        NaN          NaN   \n",
       "2       NaN         NaN              NaN          NaN        NaN          NaN   \n",
       "3       NaN         NaN              NaN          NaN        NaN          NaN   \n",
       "4       NaN         NaN              NaN          NaN        NaN          NaN   \n",
       "\n",
       "   d_month  last_ddate  trade_time_desc  \n",
       "0      NaN         NaN              NaN  \n",
       "1      NaN         NaN              NaN  \n",
       "2      NaN         NaN              NaN  \n",
       "3      NaN         NaN              NaN  \n",
       "4      NaN         NaN              NaN  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_main = pd.DataFrame()\n",
    "\n",
    "for file in mainfiles:\n",
    "    df_main = df_main.append(pd.read_csv(f\"{dir}{file}.csv\"))\n",
    "df_main.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d922f492",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "143\n"
     ]
    }
   ],
   "source": [
    "print(len(df_main))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6a166d71",
   "metadata": {},
   "outputs": [],
   "source": [
    "main_columns = [\"ts_code\",\"symbol\",\"exchange\",\"name\",\"fut_code\"]\n",
    "\n",
    "df_t = df_main[main_columns].sort_values(sort_values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d789a610",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_t.to_csv(f\"{dir}all_main_contracts.csv\", index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
